package com.ayuku.retail.ui.pembelian;

import java.sql.SQLException;

import org.eclipse.nebula.widgets.formattedtext.NumberFormatter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.AyBigDecimal;
import com.ayuku.commons.AyukuDB;
import com.ayuku.commons.Util;
import com.ayuku.retail.persistence.model.TrxTempSale;

public class EditItem extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Label txtKode;
	private AyBigDecimal txtQty;
	private Label txtKeterangan;
	private String kode;
	private Label lblCode;
	private Label lblName;
	private Label lblQty;
	private Label label;
	private Button btnsave;

	public String getKode() {
		return kode;
	}

	public void setKode(String kode) {
		this.kode = kode;
	}

	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			EditItem inst = new EditItem(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public EditItem(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);
			dialogShell.setSize(457, 103);
			dialogShell.setLayout(new GridLayout(4, false));

			lblCode = new Label(dialogShell, SWT.NONE);
			lblCode.setText("Code");

			lblName = new Label(dialogShell, SWT.NONE);
			lblName.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
			lblName.setText("Name");

			lblQty = new Label(dialogShell, SWT.NONE);
			lblQty.setText("Qty");
			new Label(dialogShell, SWT.NONE);

			label = new Label(dialogShell, SWT.SEPARATOR | SWT.HORIZONTAL);
			label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
					false, 4, 1));

			txtKode = new Label(dialogShell, SWT.NONE);
			GridData gd_txtKode = new GridData(SWT.LEFT, SWT.CENTER, false,
					false, 1, 1);
			gd_txtKode.widthHint = 106;
			txtKode.setLayoutData(gd_txtKode);
			txtKode.setText("dfdf");

			txtKeterangan = new Label(dialogShell, SWT.NONE);
			txtKeterangan.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
			txtKeterangan.setText("Keterangan");

			txtQty = new AyBigDecimal(dialogShell, SWT.BORDER);

			txtQty.setFormatter(new NumberFormatter("#,##0.#"));
			txtQty.setTextLimit(10);
			GridData gd_txtQty = new GridData(SWT.FILL, SWT.CENTER, false,
					false, 1, 1);
			gd_txtQty.widthHint = 58;
			txtQty.setLayoutData(gd_txtQty);

			txtQty.getOrgComponent().getControl().addKeyListener(
					new KeyAdapter() {
						public void keyPressed(KeyEvent evt) {
							txtQtyKeyPressed(evt);
						}
					});

		
			btnsave = new Button(dialogShell, SWT.NONE);
			btnsave.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent arg0) {
					doSave();
					dialogShell.dispose();
				}
			});
			btnsave.setText("&Save");
			
			loadData();

			dialogShell.open();
			dialogShell.layout();
			Util.setCenter(dialogShell);

			txtQty.setFocus();
			txtQty.getOrgComponent().getControl().selectAll();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doSave() {
		TrxTempSale sale = null;
		try {
			sale = AyukuDB.getPersist().readByPrimaryKey(TrxTempSale.class,
					txtKode.getText().trim());
			if (sale != null) {
				sale.setQty(txtQty.getValue());
				AyukuDB.getPersist().update(sale);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void txtQtyKeyPressed(KeyEvent evt) {
		if (evt.keyCode == SWT.CR) {
			doSave();
			dialogShell.dispose();
		}
	}

	private void loadData() throws SQLException {
		TrxTempSale sale = AyukuDB.getPersist().readByPrimaryKey(
				TrxTempSale.class, this.kode);
		txtKode.setText(sale.getBarcode());
		txtKeterangan.setText(sale.getKeterangan());
		txtQty.setValue(sale.getQty());
	}

}
